package vip.xiaonuo.megvii.facegroups.delete.impl;

import com.joe.util.GsonUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.facedoor.modular.memberfeatures.entity.MemberFeatures;
import vip.xiaonuo.facedoor.modular.memberfeatures.service.MemberFeaturesService;
import vip.xiaonuo.megvii.facegroups.delete.FaceGroupDeleteStrategy;
import vip.xiaonuo.modular.facegroupfacetokenrela.entity.FacegroupFacetokenRela;
import vip.xiaonuo.modular.facegroupfacetokenrela.param.FacegroupFacetokenRelaParam;
import vip.xiaonuo.modular.facegroupfacetokenrela.service.FacegroupFacetokenRelaService;
import vip.xiaonuo.modular.facegrouporgrelate.service.FacegroupOrgRelateService;
import vip.xiaonuo.modular.megviifacegroup.entity.MegviiFaceGroup;
import vip.xiaonuo.modular.megviifacegroup.service.MegviiFaceGroupService;
import vip.xiaonuo.modular.megviifacegroup.service.impl.MegviiFaceGroupServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 以分组下人脸为切入点，删除所有关联
 * 再删除人脸，
 * 最后删除分组
 */
@Service
public class DeleteFaceGroupDropFace extends DeleteFaceGroupBaseService implements FaceGroupDeleteStrategy {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(MegviiFaceGroup group) {
        // 所有人脸
        List<MemberFeatures> features = faceGroupService.listMemberFeatureByGroupId(group.getId());

        List<FacegroupFacetokenRela> toDelete = new ArrayList<>();
        for (MemberFeatures feature : features){
            // 删除该人脸关联的分组
            FacegroupFacetokenRelaParam param = new FacegroupFacetokenRelaParam();
            param.setFeaturesId(feature.getId());
            List<FacegroupFacetokenRela> relas = relaService.list(param);

            toDelete.addAll(relas);
        }

        relaService.removeBatchByIds(toDelete);
        featuresService.removeBatchByIds(features);
        // 删除分组
        group.setDelFlag(1);
        groupService.removeById(group);

        // 删除分组关联
        this.deleteFaceGroupOrgRelate(groupOrgRelateService,group.getId());
    }
}
